package com.mybatisplus.demo;


import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.github.pagehelper.Page;
import com.mybatisplus.demo.mapper.UserMapper2;
import com.mybatisplus.demo.model.MyPage;
import com.mybatisplus.demo.model.ParamSome;
import com.mybatisplus.demo.model.User;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.Date;
import java.util.List;

import static com.github.pagehelper.page.PageMethod.startPage;

@RunWith(SpringRunner.class)
@SpringBootTest
public class SampleTest {

    @Autowired
    private UserMapper2 userMapper2;

    @Test
    public void testSelect() {
        System.out.println(("----- selectAll method test ------"));
        List<User> userList = userMapper2.selectList(null);

        Assert.assertEquals(5, userList.size());
        userList.forEach(System.out::println);
    }


    @Test
    public void mySelectPage() {
        MyPage<User> myPage = new MyPage<User>(1, 5).setSelectInt(20).setSelectStr("Jack");
        ParamSome paramSome = new ParamSome(20, "Jack");
        MyPage<User> userMyPage = userMapper2.mySelectPage(myPage, paramSome);
        // assertThat(myPage).isSameAs(userMyPage);
        System.out.println("hello :" + null);
        System.out.println("总条数 -------------> {}" + userMyPage.getTotal());
        System.out.println("当前页数 -------------> {}" + userMyPage.getCurrent());
        System.out.println("当前每页显示数 -------------> {}" + userMyPage.getSize());

    }


    //单独测试 pagehelper
    @Test
    public void testPagehelper() {
        Page<User> page = startPage(2, 6);
        List<User> list = userMapper2.listPage();
        int count = (int) page.getTotal();


        System.out.println("总条数 -------------> {}" + count);
        System.out.println("当前页数 -------------> {}" + page.getPageNum());
        System.out.println("当前每页显示数 -------------> {}" + page.getPageSize());

    }

    //测试更新
    @Test
    public void testupdatelock() {

        User user = userMapper2.selectById(5);
        Long id = user.getId();
        int versionOld = user.getAge();

        UpdateWrapper<User> uw = new UpdateWrapper<>();
        //更新name+age
        uw.set("name", "新的name10086");
        uw.set("age", versionOld + 1);
        //where条件
        uw.eq("id", id);
        uw.eq("age", versionOld);

        int i = userMapper2.update(null, uw);

        System.out.println("更新结果 i :" + i);


    }


}
